package com.canbot.u05.activity.reception;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;

/**
 * 迎宾模式欢迎语音设置界面
 */
public class ReceptionModeSettingSpeechActivity extends ReceptionBaseActivity implements View.OnClickListener {

        private RelativeLayout rlExistAudio, rlTextToSpeech, rlExistZhuanYeAudio;

        private TextView tvTitle;

        private ImageView ivBack;

        private Button btnFinish;

        private CheckBox checkBoxAudio, checkBoxTextToSpeech, checkBoxZhuanYeAudio;

        public static final int ZHUANYE_AUDIO_TYPE = 0;

        public static final int OTHER_AUDIO_TYPE = 1;

        private RelativeLayout rlSpeackToVoice;

        private CheckBox checkBoxSpeachToVoice;

        private int voiceType;

        private ToastTools toastTools;

        private String voiceContent;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_setting_speech);
                toastTools = ToastTools.newInstance(this);
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.USHER_DIR_PATH + "hyyysz_ybms_u05_001.wav");
                setViews();
        }

        private void setViews() {
                tvTitle = (TextView) findViewById(R.id.textView_title);
                ivBack = (ImageView) findViewById(R.id.imageView_back);
                btnFinish = (Button) findViewById(R.id.usher_mode_setting_speech_btn_finish);
                rlExistAudio = (RelativeLayout) findViewById(R.id.rl_audio);
                rlExistZhuanYeAudio = (RelativeLayout) findViewById(R.id.rl_zhunaye_audio);
                rlTextToSpeech = (RelativeLayout) findViewById(R.id.rl_text_to_speech);
                rlSpeackToVoice = (RelativeLayout) findViewById(R.id.rl_speech_to_voice);
                checkBoxAudio = (CheckBox) findViewById(R.id.cbox_usher_mode_choose_exist_audio);
                checkBoxZhuanYeAudio = (CheckBox) findViewById(R.id.cbox_usher_mode_choose_exist_zhunaye_audio);
                checkBoxTextToSpeech = (CheckBox) findViewById(R.id.cbox_usher_mode_text_to_speech);
                checkBoxSpeachToVoice = (CheckBox) findViewById(R.id.cbox_usher_mode_speech_to_voice);
                btnFinish.setOnClickListener(this);
                ivBack.setOnClickListener(this);
                rlExistAudio.setOnClickListener(this);
                rlTextToSpeech.setOnClickListener(this);
                rlSpeackToVoice.setOnClickListener(this);
                rlExistZhuanYeAudio.setOnClickListener(this);
                tvTitle.setText("欢迎语音设置");
                voiceType = getIntent().getIntExtra("voiceType", -1);
                voiceContent = getIntent().getStringExtra("voiceContent");
                setCheckbox(voiceType);
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);

                                break;
                        case R.id.usher_mode_setting_speech_btn_finish:
                                //完成
                                updateVoiceInfo(voiceType, voiceContent);
                                break;
                        case R.id.rl_audio:
                                //跳转到选择音频界面
                                Intent intent = new Intent(this, ReceptionModeChooseAudioActivity.class);
                                intent.putExtra("type", OTHER_AUDIO_TYPE);
                                if (voiceType == OTHER_AUDIO_TYPE){
                                        intent.putExtra("voiceContent",voiceContent);
                                }
                                startAudioActivity(intent, 101);
                                break;
                        case R.id.rl_zhunaye_audio:
                                //跳转到选择音频界面
                                intent = new Intent(this, ReceptionModeChooseAudioActivity.class);
                                intent.putExtra("type", ZHUANYE_AUDIO_TYPE);
                                if (voiceType == ZHUANYE_AUDIO_TYPE){
                                        intent.putExtra("voiceContent",voiceContent);
                                }
                                startAudioActivity(intent, 101);
                                break;
                        case R.id.rl_text_to_speech:
                                //跳转到文字转语音界面
                                intent = new Intent(this, ReceptionModeTextToSpeechActivity.class);
                                if (voiceType == 2){
                                        intent.putExtra("voiceContent",voiceContent);
                                }
                                startAudioActivity(intent, 100);
                                break;
                        case R.id.rl_speech_to_voice:
                                //跳转到文字转语音界面
                                intent = new Intent(this, ReceptionModeSpeakToVoiceActivity.class);
                                startAudioActivity(intent, 102);
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if ((requestCode == 100 && resultCode == RESULT_OK)
                        || (requestCode == 101 && resultCode == RESULT_OK)
                        || (requestCode == 102 && resultCode == RESULT_OK)) {
                        voiceType = data.getIntExtra("voiceType", -1);
                        voiceContent = data.getStringExtra("voiceContent");
                        setCheckbox(voiceType);
                }
        }

        private void setCheckbox(int type) {
                switch (type) {
                        case 0:
                                checkBoxTextToSpeech.setChecked(false);
                                checkBoxAudio.setChecked(false);
                                checkBoxZhuanYeAudio.setChecked(true);
                                checkBoxSpeachToVoice.setChecked(false);
                                break;
                        case 1:
                                checkBoxTextToSpeech.setChecked(false);
                                checkBoxAudio.setChecked(true);
                                checkBoxZhuanYeAudio.setChecked(false);
                                checkBoxSpeachToVoice.setChecked(false);
                                break;
                        case 2:
                                checkBoxTextToSpeech.setChecked(true);
                                checkBoxAudio.setChecked(false);
                                checkBoxZhuanYeAudio.setChecked(false);
                                checkBoxSpeachToVoice.setChecked(false);
                                break;
                        case 3:
                                checkBoxTextToSpeech.setChecked(false);
                                checkBoxAudio.setChecked(false);
                                checkBoxZhuanYeAudio.setChecked(false);
                                checkBoxSpeachToVoice.setChecked(true);
                                break;
                        default:
                                checkBoxTextToSpeech.setChecked(false);
                                checkBoxAudio.setChecked(false);
                                checkBoxZhuanYeAudio.setChecked(false);
                                checkBoxSpeachToVoice.setChecked(false);
                                break;
                }
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startOtherActivity(int code, Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startAudioActivity(Intent intent, int code) {
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }
        private Dialog dialogLoading;
        private void updateVoiceInfo(int type, String content) {
                if (type == -1) {
                        toastTools.showText(getString(R.string.select_welcome_voice));
                        return;
                }
                if (type != 3) {
                        if (TextUtils.isEmpty(content)) {
                                toastTools.showText(getString(R.string.please_set_welcome_voice));
                                return;
                        }
                }
                String url = IpMsgConst.HTTP_URL_CHEST + "usherModeAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 2);
                hashMap.put("voiceType", type);
                hashMap.put("voiceContent", content);

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.upload_complete));
                                finish();
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.network_error));
                        }
                });
        }

}
